package org.bukkit.pm.rspawn;

import java.util.logging.Level;

import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerMoveEvent;
/**
 * Handle events for all Player related events
 * such as commands
 * @author patamau
 */
public class RSpawnPlayerListener extends PlayerListener {
    private final RSpawnPlugin _plugin;

    public RSpawnPlayerListener(RSpawnPlugin plugin) {
        _plugin = plugin;
    }
    
    public void onPlayerJoin(PlayerEvent event){
    	Player p = event.getPlayer();
    	p.sendMessage("[RSpawn "+RSpawnPlugin.VERSION+"] type /rshelp for available commands");
    }
    
    public void onPlayerTeleport(PlayerMoveEvent event){
    	final Player p = event.getPlayer();
    	if(p.getHealth()>0) return; //standard teleport or already handled
		p.setHealth(20); //set full health to prevent further calls to this
		final Location l = _plugin.getSpawnLocation(p);
		//load spawn location if any
		if(l!=null){		
			event.setCancelled(true); //prevent further notifications
			//start deferred teleporting 
			Thread t = new Thread(){
				public void run(){
					try {
						Thread.sleep(RSpawnPlayer.SPAWN_TIMEOUT);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
	    			_plugin.log(Level.INFO, "teleporting "+p.getName()+" to "+l.toString());
	    			p.teleportTo(l); //move player to spawn
	    			p.setHealth(20); //restore health losses caused by traps or other before the teleport
	    			RSpawnPlayer rp = _plugin.getRSpawnPlayer(p);
	    			if(rp.getType()==RSpawnPlayer.T_HOME){
	    				p.sendMessage("[RSpawn] You've been teleported home");
	    			}else{
	    				p.sendMessage("[RSpawn] You've been teleported to a random location");
	    			}
				}
			};
			t.start();
		}
		//else, let the user spawn in the usual way
    }
    
	@Override
    public void onPlayerCommand(PlayerChatEvent event)  {
    	Player player = event.getPlayer();
        String[] split = event.getMessage().split(" ");
        if(split.length<1) {
        	_plugin.log(Level.WARNING,"command received, but no split available: "+event.getMessage());
        	return; //XXX: is it possible?
        }
        String cmd = split[0];
        event.setCancelled(true); //prevent other plugins or the server to handle this
        if(cmd.equalsIgnoreCase("/rshelp")){
        	//print a list of available commands
        	player.sendMessage("[RSpawn] command list:");
        	player.sendMessage(" /rs home: set the current location as the next spawn point");
        	player.sendMessage(" /rs random: randomize the next spawn point (surface spawn only)");
        	player.sendMessage(" /rs default: back to default spawn point");
        	if(player.isOp()){
        		//op commands here
        		player.sendMessage(" /rs teleport: teleport to recorded spawn point (Op only)");
        		player.sendMessage(" /rs current: shows current location (Op only)");
        		player.sendMessage(" /rs reset or clear: removes all recorded spawn points (Op only)");
        	}
        }else if(cmd.equalsIgnoreCase("/rs")){
        	String arg = split.length>1?split[1]:"";
        	if(arg.equalsIgnoreCase("home")){
        		player.sendMessage("[RSpawn] Your spawn point is now set at the current location");
        		_plugin.log(Level.INFO, player.getName()+" set home at "+player.getLocation().toString());
        		_plugin.setHomeSpawn(player, player.getLocation());
        	}else if(arg.equalsIgnoreCase("default")){
        		player.sendMessage("[RSpawn] Default spawn point selected");
        		_plugin.log(Level.INFO, player.getName()+" set respawn to default");
        		_plugin.setDefaultSpawn(player);
        	}else if(arg.equalsIgnoreCase("random")){
        		player.sendMessage("[RSpawn] Your spawn point is now randomic");
        		_plugin.log(Level.INFO, player.getName()+" set respawn to random");
        		_plugin.setRandomSpawn(player);
        	}else if(player.isOp()){
        		if(arg.equalsIgnoreCase("teleport")){
	        		Location loc = _plugin.getSpawnLocation(player);
	        		if(loc==null){
	        			player.sendMessage("[RSpawn] cannot teleport: no destination specified");
	        		}else{
	        			player.teleportTo(loc);
	        		}
	        	}else if(arg.equalsIgnoreCase("current")){
	        		Location loc = player.getLocation();
	        		if(loc!=null){
	        			player.sendMessage("[RSpawn] current location is ("+loc.getX()+","+loc.getY()+","+loc.getZ()+")");
	        		}
	        	}else if(arg.equalsIgnoreCase("reset")||arg.equalsIgnoreCase("clear")){
	        		_plugin.reset();
	        	}
        	}else{
        		player.sendMessage("[RSpawn] wrong option, /rshelp for a list of valid options");
        	}
        }else{
        	//restore event if it cannot be handled by this plugin
        	event.setCancelled(false);
        }
    }
}
